/*
 *
 * This file is part of the open-source SeetaFace engine, which includes three modules:
 * SeetaFace Detection, SeetaFace Alignment, and SeetaFace Identification.
 *
 * This file is part of the SeetaFace Detection module, containing codes implementing the
 * face detection method described in the following paper:
 *
 *
 *   Funnel-structured cascade for multi-view face detection with alignment awareness,
 *   Shuzhe Wu, Meina Kan, Zhenliang He, Shiguang Shan, Xilin Chen.
 *   In Neurocomputing (under review)
 *
 *
 * Copyright (C) 2016, Visual Information Processing and Learning (VIPL) group,
 * Institute of Computing Technology, Chinese Academy of Sciences, Beijing, China.
 *
 * The codes are mainly developed by Shuzhe Wu (a Ph.D supervised by Prof. Shiguang Shan)
 *
 * As an open-source face recognition engine: you can redistribute SeetaFace source codes
 * and/or modify it under the terms of the BSD 2-Clause License.
 *
 * You should have received a copy of the BSD 2-Clause License along with the software.
 * If not, see < https://opensource.org/licenses/BSD-2-Clause>.
 *
 * Contact Info: you can send an email to SeetaFace@vipl.ict.ac.cn for any problems.
 *
 * Note: the above information must be kept whenever or wherever the codes are used.
 *
 */

#include "lab_boost_model_reader.h"


void InitLABBoostModelReader(struct classLABBoostModelReader *p)
{

}

bool LABBoostModelReaderRead(struct classLABBoostModelReader *p, FILE* input, LABBoostedClassifier* model)
{
  bool is_read;
 // LABBoostedClassifier* lab_boosted_classifier = (LABBoostedClassifier*)(model);

  fread((&p->num_base_classifer_), sizeof(int32_t),1,input);
  fread((&p->num_bin_), sizeof(int32_t),1,input);

#if USGPU
  int32_t weight_len = sizeof(float)* (num_bin_ + 1);
  lab_boosted_classifier->FeatInitGPU((num_bin_ + 1), num_base_classifer_);
#endif
  is_read = (input != NULL) && p->num_base_classifer_ > 0 && p->num_bin_ > 0 &&
	  LABBoostModelReaderReadFeatureParam(p, input, model) &&
	  LABBoostModelReaderReadBaseClassifierParam(p, input, model);

  return is_read;
}

bool LABBoostModelReaderReadFeatureParam(struct classLABBoostModelReader *p, FILE* input, LABBoostedClassifier* model) {
	int i;
	int32_t x = 0;
	int32_t y = 0;
	FILE* newtest ;
	int len = ftell(input);
  for ( i = 0; i < p->num_base_classifer_; i++) {
	  if (fread(&x,sizeof(int32_t), 1, input) == 0) printf("less x\n");
	  if (fread(&y, sizeof(int32_t),1, input) == 0) printf("less y\n");
	  //printf("%d:x=%d,y=%d\n",i,x,y);
#if USGPU
	model->AddFeatureGPU(model,x, y, i);
#else
	LABBoostedClassifierAddFeature(model, x, y,i);
#endif
  }
  return !(input == NULL);
}

bool LABBoostModelReaderReadBaseClassifierParam(struct classLABBoostModelReader *p, FILE* input, LABBoostedClassifier* model) {
  //thresh.resize(p->num_base_classifer_);
  int i;
  float* thresh = (float*)malloc(sizeof(float)*p->num_base_classifer_);
  fread((char*)(thresh),
    sizeof(float), p->num_base_classifer_,input);

  int32_t weight_len = sizeof(float)* (p->num_bin_ + 1);
  float* weights = (float*)malloc(sizeof(float)*(p->num_bin_ + 1));
  //weights.resize(p->num_bin_ + 1);

  for ( i = 0; i < p->num_base_classifer_; i++) {
	  fread((weights), sizeof(float), p->num_bin_ + 1, input);
#if USGPU
	model->AddBaseClassifierGPU(weights.data(), num_bin_, thresh[i],i);
#else
	LABBoostedClassifierAddBaseClassifier(model, weights, p->num_bin_, thresh[i],i);
#endif
  }

  return !(input == NULL);
}

